package com.sj;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/4/23
 * Time: 下午 14:12
 * Description: 代码生成器
 */
public class GenerateCode {

  public static void main(String[] args) {
    AutoGenerator ag = new AutoGenerator();

    //全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java")
      .setAuthor("小七")
      .setOpen(false) // 打开资源管理器
      .setFileOverride(false) //是否覆盖已存在文件
      .setServiceName("%sService") // 去除service接口前的 I
      .setIdType(IdType.AUTO) // IdType
      .setDateType(DateType.ONLY_DATE) // 时间类型，仅仅是时间
      .setSwagger2(true); // 生成swagger文档
    ag.setGlobalConfig(gc);

    // 设置数据源
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/sjblog?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai")
      .setDriverName("com.mysql.cj.jdbc.Driver")
      .setUsername("root")
      .setPassword("sunjun")
      .setDbType(DbType.MYSQL);
    ag.setDataSource(dsc);

    // 包的配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.sj")
      .setController("controller")
      .setEntity("entity")
      .setService("service")
      .setMapper("mapper");
    ag.setPackageInfo(pc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setInclude("blog"); // 设置要映射的表名
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true); // 自动生成lombok
    strategy.setRestControllerStyle(true);
    strategy.setLogicDeleteFieldName("status");

    // 自动填充字段
    ArrayList<TableFill> tableFills = new ArrayList<>();
    TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
    TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
    tableFills.add(createTime);
    tableFills.add(updateTime);
    strategy.setTableFillList(tableFills);

    // 乐观锁
    strategy.setVersionFieldName("version");

    // api RestFul风格
    strategy.setRestControllerStyle(true);
    // api Rest下划线命名
    strategy.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2

    ag.setStrategy(strategy);

    ag.execute();
  }
}